<?php
/**
 *
 * @Author nikm $
 *
 * @Date 2012-01-01 $
 */

if(!defined('FROM_INDEX')) exit('Invalid request');

require ROOT_PATH.'module/master.mod.php';

class moduleObj extends masterObj
{
	function __construct($request)
	{
		parent::__construct($request);
				
		$this->excute();
	}
	
	private function excute()
	{
		ob_start();
		
		switch($this->act)
		{
			case 'login':
				$this->login();
				break;
			case 'do_login':
				$this->doLogin();
				break;
			case 'logout':
				$this->logout();
				break;
			default:
				$this->login();
				break;
		}
		
		$body = ob_get_clean();
		
		$this->showBody($body);
	}
	
	private function login()
	{
		$title = "用户登录";
		$action = "do_login";
		
		include template("login");
	}
	
	private function doLogin()
	{
		$username = isset($this->request['username']) ? trim($this->request['username']) : '';
		$password = isset($this->request['password']) ? trim($this->request['password']) : '';
		
		if($username=="" || $password=="")
		{
			$this->messager("无法登录,用户名或密码不能为空");
		}

		$user = get_user(array('name'=>$username));
		
		if(isset($user['id']) && $user['id'] > 0)
		{
			$validate_pwd = $this->createPassword($user['create_date'], $password);
						
			if($user['password'] == $validate_pwd)
			{
				unset($user['password']);
				unset($user['create_date']);
				
				$items = get_user_travel_item($user['id']);
				
				$item_list = array();
				if(check_array($items))
				{
					$temp = array();
					foreach($items as $item)
					{
//						if($item['type'] == 'sc')
//						{
//							update_scenic_favorite_count(array('id'=>$item['item_id']));
//						}
						
						$temp['item_id'] = $item['item_id'];
						$temp['type'] = $item['type'];
						
						$item_list[] = $temp;
					}
				}
				$user['item_list'] = $item_list;
				
				$_SESSION['user'] = $user;
	
				$this->messager("登录成功", 'index.php');
			}
		}
		else
		{
			$this->messager("无法登录,用户不存在。");
		}

		$this->messager('登录失败', null);
	}
	
	private function logout()
	{
		unset($_SESSION['user']);
		
		$this->messager('您已经成功退出', 'index.php');
	}
}